﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NEXEntidad;

public partial class WebControls_ResultadoLibros : System.Web.UI.UserControl
{
    #region Campos / Propiedades
    private LibroCollection fLibros;

    public LibroCollection Libros
    {
        get { return fLibros; }
        set { fLibros = value; }
    }
    #endregion

    #region Eventos Formulario


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //String strScript = "";
            //strScript = "$(document).ready(function() {$('input:radio:not([leido])').checkbox();$('input[leido]:radio').checkbox({cls:'jquery-leido-checkbox'});$('input[leyendo]:radio').checkbox({cls:'jquery-leyendo-checkbox'});$('input[quieroLeer]:radio').checkbox({cls:'jquery-quieroleer-checkbox'});	});";
            //ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ConterScript", strScript,true);

        }
    }

    protected void btnPaginas_Click(object sender, EventArgs e)
    {

        int CurPage;
        if (((LinkButton)sender).Attributes["Pagina"] != null)
            CurPage = Convert.ToInt32(((LinkButton)sender).Attributes["pagina"]);
        else
            CurPage = 1;

        

    }
    #endregion

    #region Metodos Privados
    public void PaginarEditoriales(int PaginaActual, LibroCollection libros)
    {
        PagedDataSource pdDataSource = new PagedDataSource();
        int paginaElegida;

        pdDataSource.DataSource = libros;
        pdDataSource.AllowPaging = true;
        pdDataSource.PageSize = 10;

        pdDataSource.CurrentPageIndex = PaginaActual - 1;
        paginaElegida = PaginaActual - 1;

        repeaterLibro.DataSource = pdDataSource;
        repeaterLibro.DataBind();


        if (pdDataSource.PageCount > 0)
            if (pdDataSource.PageCount > 1)
            {
                TableRow fila= new TableRow();

                AgregarCeldaTexto(fila, "ANTERIOR");
                AgregarCeldaTexto(fila, " - ");
                
                //for (int indice = 1; indice < pdDataSource.PageCount; indice++)
                for (int indice = 1; indice < 10; indice++)
                {
                    AgregarCeldaHyper(fila, indice.ToString(),indice);
                    AgregarCeldaTexto(fila, " - ");
                }
                AgregarCeldaTexto(fila, "POSTERIOR");
                
                tblPaginado.Rows.Add(fila);

                //lblPaginas.Visible = true;
                //lblPaginas.Text = "(Pag. " + PaginaActual.ToString() + " de " + pdDataSource.PageCount.ToString() + ")";
                //if (!pdDataSource.IsLastPage)
                //{
                //    btnSiguiente.Visible = true;
                //    btnSiguiente.Attributes["Pagina"] = Convert.ToString(PaginaActual + 1);
                //}
                //else
                //    btnSiguiente.Visible = false;
            }
            else
            {
                //lblPaginas.Visible = false;
                //lblPaginas.Text = "";
            }

    }

    private void AgregarCeldaTexto(TableRow fila, String valor)
    {
        try
        {
            TableCell celda = new TableCell();
            celda.Text = valor;
            fila.Cells.Add(celda);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void AgregarCeldaHyper(TableRow fila, String valor,int pagina)
    {
        try
        {
            LinkButton lnkBoton = new LinkButton();
            lnkBoton.Attributes.Add("Pagina", pagina.ToString());
            lnkBoton.Text = valor;
            lnkBoton.ID="lnkBoton"+ valor;
            lnkBoton.Click += new EventHandler(LinkButton1_Click);

            TableCell celda = new TableCell();
            celda.Controls.Add(lnkBoton);
            fila.Cells.Add(celda);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int a = 0;
        a = a+1;
    }
    protected void repeaterLibro_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                String strScript = "";

                strScript = "$(document).ready(function() { ";
                strScript += "  $('input:radio:not([leido])').checkbox(); ";
                strScript += "  $('input[leido]:radio').checkbox({cls:'jquery-leido-checkbox'}); ";
                strScript += "  $('input[leyendo]:radio').checkbox({cls:'jquery-leyendo-checkbox'}); ";
                strScript += "  $('input[quieroLeer]:radio').checkbox({cls:'jquery-quieroleer-checkbox'});	";
                strScript += "  $($('input:radio')).click(function() { ";
                strScript += "  alert('hola');";
                strScript += "      $.ajax({ ";
                strScript += "          type: 'POST', ";
                strScript += "          url: 'ListaLibro.aspx/WebListarLibros', ";
                strScript += "          /*data: {'titulo':'holaestoesajax'}, */";
                strScript += "          contentType: 'application/json; charset=utf-8', ";
                strScript += "          dataType: 'json', ";
                strScript += "          success: function(msg) { ";
                strScript += "               /*$('#Result').text(msg.d); */";
                strScript += "          } ";
                strScript += "      }); ";
                strScript += "  }); ";
                strScript += "}); ";


                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ConterScript", strScript, true);

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
